﻿Imports System.Text

Public Class RequestBuilder
    ''' <summary>
    ''' This function creates the query string for purifying negative resources. 
    ''' </summary>
    ''' <returns>The string for purifying resources.</returns>
    ''' <remarks></remarks>
    Public Shared Function createContent(ByVal cityID As String, ByVal SessionID As String, ByVal CheckBoxWood As Boolean, ByVal CheckBoxStone As Boolean, ByVal CheckBoxIron As Boolean, ByVal CheckBoxWheat As Boolean) As String
        Dim contentString As New StringBuilder("")
        Dim resourceString As New StringBuilder("")

        contentString.Append("{""session"":""" + SessionID + """,""cityid"":""" + cityid + """,""res"":[")

        If CheckBoxWood Then
            resourceString.Append("{""t"":1,""c"":-999}")
        End If
        If CheckBoxStone Then
            If resourceString.Length > 0 Then
                resourceString.Append(",")
            End If
            resourceString.Append("{""t"":2,""c"":-999}")
        End If
        If CheckBoxIron Then
            If resourceString.Length > 0 Then
                resourceString.Append(",")
            End If
            resourceString.Append("{""t"":3,""c"":-999}")
        End If
        If CheckBoxWheat Then
            If resourceString.Length > 0 Then
                resourceString.Append(",")
            End If
            resourceString.Append("{""t"":4,""c"":-999}")
        End If

        contentString.Append(resourceString)
        contentString.Append("]}")

        Return contentString.ToString
    End Function

    ''' <summary>
    ''' This is an overloaded function that uses city object to make sure there is still enough space in the city to put resources.
    ''' </summary>
    ''' <param name="city">An object containing the number of resources needed to fill up the city</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function createContent(ByVal SessionID As String, ByVal city As CityClass) As String
        Dim contentString As New StringBuilder("")
        Dim resourceString As New StringBuilder("")

        contentString.Append("{""session"":""" + SessionID + """,""cityid"":""" + city.cityID + """,""res"":[")

        If city.getWood > 0 Then
            resourceString.Append("{""t"":1,""c"":-999}")
            city.setWood(city.getWood - 1)
        End If
        If city.getStone > 0 Then
            If resourceString.Length > 0 Then
                resourceString.Append(",")
            End If
            resourceString.Append("{""t"":2,""c"":-999}")
            city.setStone(city.getStone - 1)
        End If
        If city.getIron > 0 Then
            If resourceString.Length > 0 Then
                resourceString.Append(",")
            End If
            resourceString.Append("{""t"":3,""c"":-999}")
            city.setIron(city.getIron - 1)
        End If
        If city.getWheat > 0 Then
            If resourceString.Length > 0 Then
                resourceString.Append(",")
            End If
            resourceString.Append("{""t"":4,""c"":-999}")
            city.setWheat(city.getWheat - 1)
        End If

        contentString.Append(resourceString)
        contentString.Append("]}")

        Return contentString.ToString
    End Function

    ''' <summary>
    ''' This function creates the query string for purifying negative resources. 
    ''' </summary>
    ''' <returns>The string for purifying resources.</returns>
    ''' <remarks></remarks>
    Public Shared Function PurifyResources(ByVal SessionID As String, ByVal CheckBoxWood As Boolean, ByVal CheckBoxStone As Boolean, ByVal CheckBoxIron As Boolean, ByVal CheckBoxWheat As Boolean, ByVal city As CityClass, ByVal resToPurify As Integer) As String
        Dim contentString As New StringBuilder("")
        Dim resourceString As New StringBuilder("")

        contentString.Append("{""session"":""" + SessionID + """,""cityid"":""" + city.cityID + """,""res"":[")

        If CheckBoxWood Then
            resourceString.Append("{""t"":1,""c"":" & resToPurify & "}")
        End If
        If CheckBoxStone Then
            If resourceString.Length > 0 Then
                resourceString.Append(",")
            End If
            resourceString.Append("{""t"":2,""c"":" & resToPurify & "}")
        End If
        If CheckBoxIron Then
            If resourceString.Length > 0 Then
                resourceString.Append(",")
            End If
            resourceString.Append("{""t"":3,""c"":" & resToPurify & "}")
        End If
        If CheckBoxWheat Then
            If resourceString.Length > 0 Then
                resourceString.Append(",")
            End If
            resourceString.Append("{""t"":4,""c"":" & resToPurify & "}")
        End If

        contentString.Append(resourceString)
        contentString.Append("]}")

        Return contentString.ToString
    End Function
End Class
